package com.guxingke.gblog.action;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.guxingke.gblog.dmo.FriendLink;
import com.guxingke.gblog.dto.ArticleDto;
import com.guxingke.gblog.dto.ArticleLiteDto;
import com.guxingke.gblog.dto.CategoryDto;
import com.guxingke.gblog.dto.MsgDto;
import com.guxingke.gblog.service.ArticleService;
import com.guxingke.gblog.service.CategoryService;
import com.guxingke.gblog.service.FriendLinkService;
import com.guxingke.gblog.service.MsgService;
import com.guxingke.gblog.service.impl.ArticleServiceImpl;
import com.guxingke.gblog.service.impl.CategoryServiceImpl;
import com.guxingke.gblog.service.impl.FriendLinkServiceImpl;
import com.guxingke.gblog.service.impl.MsgServiceImpl;
import com.guxingke.gblog.util.PageBean;
import com.guxingke.gblog.util.PageUtil;
import com.guxingke.gblog.util.PropertiesUtil;
import com.guxingke.gblog.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private ServletContext application;

	private ArticleService articleService = new ArticleServiceImpl();
	private MsgService msgServive = new MsgServiceImpl();
	private FriendLinkService friendLinkService = new FriendLinkServiceImpl();
	private CategoryService categoryService = new CategoryServiceImpl();

	private List<ArticleLiteDto> recentArticles;
	private List<MsgDto> recentMsgs;
	private List<FriendLink> friendLinks;
	private List<CategoryDto> categories;
	private String page;
	private List<ArticleDto> articles;
	private String pageCode;
	private String mainPage;

	@Override
	public String execute() throws Exception {
		application = request.getSession().getServletContext();
		recentArticles = articleService.getRecentArticles();
		recentMsgs = msgServive.getRecentMsgs();
		friendLinks = friendLinkService.getFriendLinks();
		categories = categoryService.getCategories();
		application.setAttribute("recentArticles", recentArticles);
		application.setAttribute("recentMsgs", recentMsgs);
		application.setAttribute("friends", friendLinks);
		application.setAttribute("categories", categories);

		if (StringUtil.isEmpty(page)) {
			page = "1";
		}

		PageBean pageBean = new PageBean(Integer.parseInt(page),
				Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		articles = articleService.getArticles(pageBean);
		int total = articleService.count();
		pageCode = PageUtil.genPagation(total, Integer.parseInt(page),
				Integer.parseInt(PropertiesUtil.getValue("pageSize")));

		mainPage = "template/article/list.jsp";
		return SUCCESS;
	}

	public List<ArticleDto> getArticles() {
		return articles;
	}

	public void setArticles(List<ArticleDto> articles) {
		this.articles = articles;
	}

	public String getPageCode() {
		return pageCode;
	}

	public void setPageCode(String pageCode) {
		this.pageCode = pageCode;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
